# JSON libraries
library(rjson)
library(RJSONIO)
library(jsonlite)
# To read the netCDF file (check for better options!)
library(ncdf4)
# To get the min bounding box
library(sp)
library(shotGroups)
# To plot the track of the glider
library(spacetime)
library(trajectories)
library(leaflet)
# To format dates
library(lubridate)
# ?? Not sure why I was using this
library(tidyverse)
ListLoadedLibraries = function(){
# Listing the default libraries. These do not need to be listed
defaultLibraries = list("stats","graphics" ,"grDevices","utils","datasets" ,"methods","base")
# Listing the loaded libraries
loadedLibraries = (.packages())
# Getting the loaded libraries only
LoadedlibrariesOnly = setdiff(loadedLibraries, defaultLibraries)
# Adding package: to the names of the libraries
libraries1= paste0("package:", LoadedlibrariesOnly)
sapply(libraries1, ls)
}
# ListLoadedLibraries()
getMinBBox = function(filePath){
}
# This is where the netCDF data files reside
netCDF_Data_Files_Path = "D:/University/WWU/WWU 5/Task 1/Reading netCDF data using R/"
# This is the files that needs to be read
netCDF_File_Name = "amerigo_coconet_R.nc"
# This is the complete path to the netCDF file that is being read
file_Path = paste0(netCDF_Data_Files_Path,netCDF_File_Name)
#Reading the file
file = nc_open(file_Path)
# Getting the latitude and longitude
lon = ncvar_get(file,"LONGITUDE")
lat = ncvar_get(file,"LATITUDE")
# Combining the lat and lon in a dataframe
lat_lon = cbind.data.frame(lat,lon)
# Changing the column names of the dataframe
names(lat_lon)[1] = "point.x"
names(lat_lon)[2] = "point.y"
# Converting the dataframe to a SpatialPoints object
lat_lon_spatial = SpatialPoints(lat_lon)
# Applying the bbox function
bb_sp = bbox(lat_lon_spatial)
head(lat_lon)
## point.x point.y
## 1 41.66751 17.06750
## 2 41.66751 17.06751
## 3 41.66751 17.06751
## 4 41.66751 17.06752
## 5 41.66752 17.06753
## 6 41.66752 17.06753
bb_sp
## min max
## point.x 41.32905 41.80003
## point.y 16.90610 17.67666
This function also returns the width and height of the bounding box. Since the projection is WGS84. This information is not useful.
bb = getBoundingBox(lat_lon)
bb
## $pts
## xleft ybottom xright ytop
## 41.32905 16.90610 41.80003 17.67666
##
## $width
## [1] 0.4709773
##
## $height
## [1] 0.7705615
##
## $FoM
## [1] 0.6207694
##
## $diag
## [1] 0.9030973
This is to check what the functions above are returning.
range(lat)
## [1] 41.32905 41.80003
range(lon)
## [1] 16.90610 17.67666
This function returns the minimum bounding box but it returns NULL. I was not able to figure out what the problem is.
x = getMinBBox(lat_lon)
x
## NULL
test = cbind(lat,lon)
y = getMinBBox(test)
y
## NULL
# Getting the time
time_D = file$dim$TIME
# Formatting the time
time_D_formatted = as.POSIXct(time_D$vals, origin="1970-01-01")
# Setting a projection
crs = CRS("+proj=longlat +datum=WGS84")
# Creating an STIDF object
stidf = STIDF(SpatialPoints(cbind(lat,lon),crs), time_D_formatted, data.frame(lat_lon))
# Creating a track object
glider_track = Track(stidf)
plot(glider_track)
# Accessing the coordinates of the Track object
# A1@data[,1]
# A1@data[,2]
# Ploting the glider track using leflet
leaflet() %>%addTiles() %>% addPolylines(lat = glider_track@data[,1], lng = glider_track@data[,2])
# Getting the time stamp of the time available in the netCDF file
timeStamp = format(time_D_formatted,'%H:%M:%S')
# Getting the date of the time available in the netCDF file
date = as.Date(time_D_formatted)
# Combining the lat, lon, time , time stamp and the date in one dataframe
lat_lon_time_dateframe = cbind.data.frame(lat, lon, time_D_formatted, timeStamp, date)
head(lat_lon_time_dateframe)
## lat lon time_D_formatted timeStamp date
## 1 41.66751 17.06750 2013-05-15 14:14:48 14:14:48 2013-05-15
## 2 41.66751 17.06751 2013-05-15 14:14:53 14:14:53 2013-05-15
## 3 41.66751 17.06751 2013-05-15 14:14:53 14:14:53 2013-05-15
## 4 41.66751 17.06752 2013-05-15 14:14:58 14:14:58 2013-05-15
## 5 41.66752 17.06753 2013-05-15 14:15:02 14:15:02 2013-05-15
## 6 41.66752 17.06753 2013-05-15 14:15:03 14:15:03 2013-05-15
# !!! I tried this but it does not work !!!
# I wanted to get the minimum date of each day in the dataframe
# test %>% group_by(time_D_formatted) %>% filter(time_D_formatted == min(time_D_formatted))
This is not effecient at all and R crashed when I tried it.
time_array = lat_lon_time_dateframe[,3]
head(time_array)
## [1] "2013-05-15 14:14:48 CEST" "2013-05-15 14:14:53 CEST"
## [3] "2013-05-15 14:14:53 CEST" "2013-05-15 14:14:58 CEST"
## [5] "2013-05-15 14:15:02 CEST" "2013-05-15 14:15:03 CEST"
#Getting the first value of the arry
first_value = time_array[1]
as.Date(ymd_hms(first_value))
## [1] "2013-05-15"
#for(i in time_array){
# as.Date(ymd_hms(i))
#}
Adding the coordinates to the addMarkers using coordinate1[1] did not work. I had to hard code the coordinates.
This section extracts the coordinates of the first value of each day manually. The labels are also manually added to the leaflet map.
# manual_labels = data.frame("lat", "lon", "time_formated")
coordinate1 = c(as.double(41.66751), as.double(17.06750), "2013-05-15 14:14:48")
coordinate2 = c("41.69140", "17.09850", "2013-05-16 02:00:00")
leaflet() %>%addTiles() %>% addPolylines(lat = glider_track@data[,1], lng = glider_track@data[,2]) %>% addMarkers(lng = 17.06750,
lat = 41.66751,
label = coordinate1[3],
labelOptions = labelOptions(noHide =T)) %>% addMarkers(lng = 17.09850, lat = 41.69140, label = coordinate2[3])
What I thought of is splitting the dataframe that has all the days in the netCDF file into seperate dataframes each containing the measurement for each day. The first row of each dataframe is then extracted to get the first measurement of that day.
# Splitting the dataframe based on date
# This results in a list of dataframes
list = split(lat_lon_time_dateframe, lat_lon_time_dateframe$date)
#This provides the first row of each day in the data frame
first_days = do.call(rbind, (lapply(list, function(x) x[1,])))
# Plotting the glider track with labels
leaflet() %>%addTiles() %>% addPolylines(lat = glider_track@data[,1], lng = glider_track@data[,2]) %>% addMarkers(data = first_days, lat = ~first_days$lat, lng = ~first_days$lon, label = first_days$time_D_formatted)
# This provides a summary of the dataframe based on the date
lat_lon_time_dateframe %>% group_split(date) %>% map(summary)
## [[1]]
## lat lon time_D_formatted
## Min. :41.67 Min. :17.05 Min. :2013-05-15 14:14:49
## 1st Qu.:41.68 1st Qu.:17.06 1st Qu.:2013-05-15 17:54:43
## Median :41.69 Median :17.07 Median :2013-05-15 20:17:24
## Mean :41.68 Mean :17.07 Mean :2013-05-15 20:12:52
## 3rd Qu.:41.69 3rd Qu.:17.08 3rd Qu.:2013-05-15 22:31:33
## Max. :41.69 Max. :17.10 Max. :2013-05-16 01:59:56
##
## timeStamp date
## 00:02:58: 2 Min. :2013-05-15
## 00:03:53: 2 1st Qu.:2013-05-15
## 00:04:13: 2 Median :2013-05-15
## 00:04:28: 2 Mean :2013-05-15
## 00:04:53: 2 3rd Qu.:2013-05-15
## 00:05:33: 2 Max. :2013-05-15
## (Other) :6359
##
## [[2]]
## lat lon time_D_formatted
## Min. :41.67 Min. :17.10 Min. :2013-05-16 02:00:01
## 1st Qu.:41.68 1st Qu.:17.11 1st Qu.:2013-05-16 07:31:51
## Median :41.70 Median :17.11 Median :2013-05-16 13:54:56
## Mean :41.71 Mean :17.11 Mean :2013-05-16 14:14:17
## 3rd Qu.:41.74 3rd Qu.:17.12 3rd Qu.:2013-05-16 21:08:16
## Max. :41.74 Max. :17.12 Max. :2013-05-17 01:59:59
##
## timeStamp date
## 00:01:02: 2 Min. :2013-05-16
## 00:01:12: 2 1st Qu.:2013-05-16
## 00:02:27: 2 Median :2013-05-16
## 00:02:37: 2 Mean :2013-05-16
## 00:04:02: 2 3rd Qu.:2013-05-16
## 00:04:12: 2 Max. :2013-05-16
## (Other) :14895
##
## [[3]]
## lat lon time_D_formatted
## Min. :41.74 Min. :16.96 Min. :2013-05-17 02:00:04
## 1st Qu.:41.77 1st Qu.:16.98 1st Qu.:2013-05-17 08:14:57
## Median :41.77 Median :17.02 Median :2013-05-17 13:58:50
## Mean :41.77 Mean :17.02 Mean :2013-05-17 14:10:15
## 3rd Qu.:41.78 3rd Qu.:17.05 3rd Qu.:2013-05-17 20:31:23
## Max. :41.79 Max. :17.10 Max. :2013-05-18 01:59:59
##
## timeStamp date
## 00:00:02: 2 Min. :2013-05-17
## 00:00:12: 2 1st Qu.:2013-05-17
## 00:01:27: 2 Median :2013-05-17
## 00:01:37: 2 Mean :2013-05-17
## 00:01:47: 2 3rd Qu.:2013-05-17
## 00:03:12: 2 Max. :2013-05-17
## (Other) :19265
##
## [[4]]
## lat lon time_D_formatted
## Min. :41.76 Min. :16.91 Min. :2013-05-18 02:00:01
## 1st Qu.:41.78 1st Qu.:16.91 1st Qu.:2013-05-18 07:23:55
## Median :41.78 Median :16.92 Median :2013-05-18 13:21:59
## Mean :41.78 Mean :16.93 Mean :2013-05-18 13:31:38
## 3rd Qu.:41.80 3rd Qu.:16.94 3rd Qu.:2013-05-18 19:25:51
## Max. :41.80 Max. :16.97 Max. :2013-05-19 01:59:56
##
## timeStamp date
## 00:00:25: 2 Min. :2013-05-18
## 00:00:40: 2 1st Qu.:2013-05-18
## 00:01:15: 2 Median :2013-05-18
## 00:02:25: 2 Mean :2013-05-18
## 00:03:05: 2 3rd Qu.:2013-05-18
## 00:04:10: 2 Max. :2013-05-18
## (Other) :20255
##
## [[5]]
## lat lon time_D_formatted
## Min. :41.61 Min. :16.95 Min. :2013-05-19 02:00:01
## 1st Qu.:41.64 1st Qu.:17.02 1st Qu.:2013-05-19 07:59:35
## Median :41.70 Median :17.08 Median :2013-05-19 13:56:07
## Mean :41.69 Mean :17.09 Mean :2013-05-19 14:00:28
## 3rd Qu.:41.72 3rd Qu.:17.15 3rd Qu.:2013-05-19 19:56:06
## Max. :41.76 Max. :17.23 Max. :2013-05-20 01:59:56
##
## timeStamp date
## 00:00:36: 2 Min. :2013-05-19
## 00:00:46: 2 1st Qu.:2013-05-19
## 00:02:01: 2 Median :2013-05-19
## 00:02:11: 2 Mean :2013-05-19
## 00:03:26: 2 3rd Qu.:2013-05-19
## 00:03:36: 2 Max. :2013-05-19
## (Other) :21596
##
## [[6]]
## lat lon time_D_formatted
## Min. :41.47 Min. :17.23 Min. :2013-05-20 02:00:01
## 1st Qu.:41.51 1st Qu.:17.29 1st Qu.:2013-05-20 07:46:55
## Median :41.54 Median :17.34 Median :2013-05-20 13:13:58
## Mean :41.54 Mean :17.34 Mean :2013-05-20 13:49:13
## 3rd Qu.:41.57 3rd Qu.:17.39 3rd Qu.:2013-05-20 20:14:30
## Max. :41.61 Max. :17.43 Max. :2013-05-21 01:59:57
##
## timeStamp date
## 00:00:54: 2 Min. :2013-05-20
## 00:01:04: 2 1st Qu.:2013-05-20
## 00:02:19: 2 Median :2013-05-20
## 00:02:29: 2 Mean :2013-05-20
## 00:03:44: 2 3rd Qu.:2013-05-20
## 00:03:54: 2 Max. :2013-05-20
## (Other) :21571
##
## [[7]]
## lat lon time_D_formatted
## Min. :41.36 Min. :17.43 Min. :2013-05-21 02:00:02
## 1st Qu.:41.39 1st Qu.:17.48 1st Qu.:2013-05-21 07:18:33
## Median :41.41 Median :17.52 Median :2013-05-21 12:45:18
## Mean :41.41 Mean :17.53 Mean :2013-05-21 13:45:00
## 3rd Qu.:41.43 3rd Qu.:17.59 3rd Qu.:2013-05-21 20:29:33
## Max. :41.47 Max. :17.63 Max. :2013-05-22 01:59:57
##
## timeStamp date
## 00:00:56: 2 Min. :2013-05-21
## 00:02:21: 2 1st Qu.:2013-05-21
## 00:03:46: 2 Median :2013-05-21
## 00:04:31: 2 Mean :2013-05-21
## 00:04:41: 2 3rd Qu.:2013-05-21
## 00:05:56: 2 Max. :2013-05-21
## (Other) :19689
##
## [[8]]
## lat lon time_D_formatted
## Min. :41.33 Min. :17.63 Min. :2013-05-22 02:00:02
## 1st Qu.:41.34 1st Qu.:17.65 1st Qu.:2013-05-22 03:26:50
## Median :41.34 Median :17.66 Median :2013-05-22 04:33:02
## Mean :41.34 Mean :17.66 Mean :2013-05-22 04:36:56
## 3rd Qu.:41.35 3rd Qu.:17.67 3rd Qu.:2013-05-22 05:39:05
## Max. :41.36 Max. :17.68 Max. :2013-05-22 07:04:49
##
## timeStamp date
## 02:00:31: 2 Min. :2013-05-22
## 02:01:26: 2 1st Qu.:2013-05-22
## 02:02:11: 2 Median :2013-05-22
## 02:02:26: 2 Mean :2013-05-22
## 02:03:16: 2 3rd Qu.:2013-05-22
## 02:24:50: 2 Max. :2013-05-22
## (Other) :4640